package com.test.death_lock;

public class AccountTransfer extends Thread{
    private Account src;
    private Account target;
    private double num;

    @Override
    public void run() {
         synchronized (src){
             System.out.println(Thread.currentThread().getName());
             src.setBalance(src.getBalance()-num);
             try {
                 Thread.sleep(100);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             synchronized (target){
                 target.setBalance(target.getBalance()+num);
                 System.out.println("转账成功");
             }
         }
    }

    public AccountTransfer(String name, Account src, Account target, double num) {
        super(name);
        this.src = src;
        this.target = target;
        this.num = num;
    }
}
